<!-- 
/*
 * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *     
 *     http://www.apache.org/licenses/LICENSE-2.0  
 *     
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.
 */
 -->
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width">
		<link rel="shortcut icon" type="image/png" href="../../img/favicon.png">
        <title>微克机器人</title>
		
	</head>
	
	<body>
		<div id="mcg_body" class="container-fluid autoHeight">
			<div class="row autoHeight">
			        <div id="usertoolbar" class="btn-group">
			        	<button id="user_create_btn" class="btn btn-default">
			        		<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>创建用户
			        	</button>
			        </div>
				<div class="col-md-12 autoHeight">

			        <table id="userListTable"
			               data-toggle="table"
			               data-height="460"
			               data-show-columns="true"
			               data-search="true" 
			               data-unique-id="userId"      
				           data-pagination="true"
				           data-page-size="5"
				           data-page-list="[5,10,20,30]"
				           data-pagination-first-text="上一页"
				           data-pagination-pre-text="上一页"
				           data-pagination-next-text="下一页"
				           data-pagination-last-text="尾页">
			            <thead>
				            <tr>
				            	<th data-field="account">用户帐号</th>
				            	<th data-field="userName">用户名称</th>
				            	<th data-field="phone">手机号</th>
				            	<th data-field="email">邮箱</th>
				            	<th data-field="sex" data-formatter="userSexFormat">性别</th>
				            	<th data-field="roleType" data-formatter="userRoleTypeFormat">角色</th>
				            	<th data-field="status" data-formatter="userStatusFormat">状态</th>
				            	<th data-field="createTime" data-formatter="userDateFormat">创建时间</th>
				            	<th data-field="createUser">创建用户</th>
				            	<th data-field="updateTime" data-formatter="userDateFormat">修改时间</th>
				            	<th data-field="updateUser">修改用户</th>
				            	<th data-field="commands" data-formatter="userCommandsFormatter">操作</th>
				            </tr>
			            </thead>
			        </table>
				</div>
			</div>
	 	</div>
	</body>
</html>
<script type="text/javascript">
var userListTable = null;
$(function () {
	$('#user_create_btn').click(function(data){
		var param = {"modalId":Math.uuid() + "_create_Modal", "userId":null, "option":{"title":"创建用户", "width":1100} };
		common.showAjaxDialog("/user/initCreateUserModal", setCreateUserDialogBtns(param), createUserModalCallBack, null, param);
	});
	userListTable = $("#userListTable").bootstrapTable({toolbar:'#usertoolbar'});
	
	queryUserList();
});

function queryUserList() {
	userListTable.bootstrapTable("removeAll");
	common.ajax({
		url : "/user/getUserList",
		type : "POST",
		data : null,
		contentType : "application/json"
	}, function(data) {
		if(data != null && data.statusCode == 1) {
			if(data.resultMap != null && data.resultMap.dataList != null) {
				for(var i=0; i<data.resultMap.dataList.length; i++) {
					userListTable.bootstrapTable('append', data.resultMap.dataList[i]);
				}
			}
		} else {
			Messenger().post({
				message: data.statusMes,
				type: "error",
				hideAfter: 5,
			 	showCloseButton: true
			});
		}
		
	});
}

function userCommandsFormatter(value, row, index) {
  	var btns = '<div class="btn-group">';
  			btns += '<button type="button" onClick="userEdit(\'' + row.userId + '\', \'' + row.account + '\');" class="btn btn-default">编辑</button>';
  			btns += '<button type="button" onClick="userDelete(\'' + row.account + '\');" class="btn btn-default">删除</button>';
  			btns += '</div>';

	return btns;
}

function userDelete(account) {
	common.ajax({
		url : "/user/userDelete",
		type : "POST",
		data : "account=" + account
	}, function(data) {
		if(data != null && data.statusCode == 1) {
			queryUserList();
			Messenger().post({
				message: data.statusMes,
				type: "success",
				hideAfter: 5,
			 	showCloseButton: true
			});
		} else {
			Messenger().post({
				message: "删除用户失败，请重试！",
				type: "error",
				hideAfter: 5,
			 	showCloseButton: true
			});
		}
		
	});
	
}

function setCreateUserDialogBtns(param) {
	return buttons = [
		{
			class: "btn btn-primary",
			text: "保存",
			click: function() {
				var _this = this;
				var mcgUser = $("#" + param.modalId + "_user_create_form").serializeJSON();
				var result = JSON.parse(mcgUser);
				common.ajax({
					url : "/user/save",
					type : "POST",
					data : JSON.stringify(result.mcgUser),
					contentType : "application/json"
				}, function(data) {
					if(data.statusCode == 1) {
						queryUserList();
						$( _this ).dialog( "destroy" );
					}
				});
			}
		},
		{
			class: "btn btn-default",			
			text: "关闭",
			click: function() {
				$( this ).dialog( "destroy" );
			}
		}
	];
	
}

function userDateFormat(value) {
	if(value == undefined || value == "undefined") {
		return "";
	}
	return common.dateUtils.getFormatDateByLong(value, "yyyy-MM-dd hh:mm:ss");
}

function userStatusFormat(status) {
	if(status == undefined || status == "undefined") {
		return "未知";
	}
	
	if(status == 1) {
		return "有效";
	} else if(status == 0) {
		return "停用";
	} else {
		return "未知";
	}
}

function userRoleTypeFormat(roleType) {
	if(roleType == undefined || roleType == "undefined") {
		return "未知";
	}
	if(roleType == 'admin') {
		return "超级管理员";
	} else if(roleType == 'default') {
		return "普通用户";
	} else {
		return "未知";
	}
}

function userSexFormat(sex) {
	if(sex == undefined || sex == "undefined") {
		return "未知";
	}
	if(sex == 1) {
		return "男";
	} else if(sex == 2) {
		return "女";
	} else {
		return "未知";
	}
}

function userEdit(userId, account) {
	var param = {"modalId":Math.uuid(), "userId":userId, "account":account, "option":{"title":"编辑用户", "width":1100} };
	common.showAjaxDialog("/user/initCreateUserModal", setCreateUserDialogBtns(param), createUserModalCallBack, null, param);
}

function createUserModalCallBack(param) {
	$(".selectpicker").selectpicker({
		noneSelectedText: "请选择",
		liveSearch: true,
		width:"100%"
	});
	
	if(param.userId == null) {
		$("#mcgUserAccount").removeAttr("readonly");
	} else {
		$("#mcgUserPwd").change(function(){
			$(this).val(common.rsaUtils.rsaEncrypt($(this).val()));
		});
		$("#mcgUserAccount").attr("readonly","readonly");
		common.ajax({
			url : "/user/getUser",
			type : "POST",
			data : "account=" + param.account
		}, function(data) {
			if(data.statusCode == 1) {
				if(data.resultMap.mcgUser != null) {
					common.formUtils.setValues(param.modalId + "_user_create_form", {"mcgUser":data.resultMap.mcgUser});
				}
				
			}
		});	
	}
	

}
</script>
